package com.demo.study.controll;

import com.demo.study.entity.User;
import com.demo.study.service.UserService;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author sz
 * @version V1.0
 * @date 2023/12/24 17:38
 * @Description: 多敲代码
 */
@RestController
@RequestMapping("/user")
@Data
public class UserController {

    private  final  UserService userService;



    @GetMapping("getOne")
    public User getUserInfoById(String userId){

        return  userService.getUserInfoById(userId);
    }


    @GetMapping("test")
    public Integer testThread(){
        List<Future<?>> futureList=new ArrayList<>();
        System.out.println("主线程执行");
        for (int i = 0; i <10 ; i++) {
            int finalI = i;
            Future<?> submit = threadPoolExecutor.submit(() -> {

                System.out.println(Thread.currentThread().getName());
                User info = userService.getUserInfoById(""+ finalI);
                return info;
            });
            futureList.add(submit);
        }
        futureList.forEach(future->{
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return  10;
    }

    private  final  ThreadPoolExecutor threadPoolExecutor;

}
